package com.xinqi.modules.user.users.third.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinqi.modules.user.third.dto.req.UserNotificationUpdateDTO;
import com.xinqi.modules.user.users.third.convert.UserNotificationConvert;
import com.xinqi.modules.user.users.third.domain.UserNotificationEntity;
import com.xinqi.modules.user.users.third.manager.UserNotificationManager;
import com.xinqi.modules.user.users.third.service.UserNotificationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.Objects;

/**
 * 个人中心通知设置表服务层实现
 *
 * @author: yyzhen
 * @date: 2023/07/17
 */
@Validated
@Service("userNotificationService")
@RequiredArgsConstructor
public class UserNotificationServiceImpl implements UserNotificationService {
    private final UserNotificationConvert userNotificationConvert;
    private final UserNotificationManager userNotificationManager;

    /**
     * 查询用户个人中心通知设置
     *
     * @Title:
     * @MethodName: initNotification
     * [userId]
     * @Return com.xinqi.modules.sys.user.user.dto.rsp.UeUserNotificationResultDTO
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/14 21:17
     */
    @Override
    public UserNotificationUpdateDTO initNotification(Long userId) {
        UserNotificationEntity notificationEntity = userNotificationManager.getOne(
            new LambdaQueryWrapper<UserNotificationEntity>()
                .eq(UserNotificationEntity::getUserId, userId));
        if (Objects.isNull(notificationEntity)) {
            //没有则初始化
            UserNotificationEntity init = new UserNotificationEntity();
            init.setUserId(userId);
            this.userNotificationManager.save(init);

            notificationEntity = userNotificationManager.getOne(
                new LambdaQueryWrapper<UserNotificationEntity>()
                    .eq(UserNotificationEntity::getUserId, userId));
        }

        return userNotificationConvert.convert(notificationEntity);
    }

    /**
     * 修改 个人中心通知设置
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateNotification(UserNotificationUpdateDTO dto) {
        UserNotificationEntity notificationEntity = userNotificationManager.getOne(
            new LambdaQueryWrapper<UserNotificationEntity>()
                .eq(UserNotificationEntity::getUserId, dto.getUserId()));
        if (Objects.isNull(notificationEntity)) {
            //没有则初始化
            UserNotificationEntity init = userNotificationConvert.update(dto);
            init.setUserId(dto.getUserId());
            return userNotificationManager.save(init);
        } else {
            UserNotificationEntity update = userNotificationConvert.update(dto);
            update.setId(notificationEntity.getId());
            return userNotificationManager.updateById(update);
        }

    }

}
